package com.lgs.okhttp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.tv_test_okhttp).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //        OkHttpClient
        new Thread(new Runnable() {
            @Override
            public void run() {
                tests();
            }
        });
    }

    private void tests() {
        Toast.makeText(this, "执行", Toast.LENGTH_SHORT).show();
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("email", "602389031@qq.com")
                .add("password", "123456")
                .add("password_confirmation", "123456")
                .add("pid", "0")
                .build();
        Request request = new Request.Builder()
                .url("http://39.106.148.53:8000/api/v1/auth/register/email")
                .post(formBody)
                .build();

        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                Log.e("okhttpLog", response.body().string());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
